{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dbbf8c59-35f1-44a7-b354-233ed5751583",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>col_name</th>\n",
       "      <th>num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  col_name  num\n",
       "0        A    3\n",
       "1        B   10"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入数据\n",
    "import pandas as pd\n",
    "\n",
    "df = pd.read_excel('./根据数字生成行.xlsx',sheet_name='test')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "41b49cf3-63c9-4ec5-a9bf-3e135efb9c1b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法一的结果：\n",
      "  col_name  num index\n",
      "0        A    3     0\n",
      "0        A    3     1\n",
      "0        A    3     2\n",
      "1        B   10     0\n",
      "1        B   10     1\n",
      "1        B   10     2\n",
      "1        B   10     3\n",
      "1        B   10     4\n",
      "1        B   10     5\n",
      "1        B   10     6\n",
      "1        B   10     7\n",
      "1        B   10     8\n",
      "1        B   10     9\n"
     ]
    }
   ],
   "source": [
    "# 方法一\n",
    "df1 = df.copy()\n",
    "df1['index'] = df1.apply(lambda x: list(range(x['num'])), axis=1)\n",
    "df_result = df1.explode('index')\n",
    "print(f'方法一的结果：\\n{df_result}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9d8c8023-6176-48da-8b96-c53b45cb4a64",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法二的结果：\n",
      "  col_name  num\n",
      "0        A    3\n",
      "0        A    3\n",
      "0        A    3\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n",
      "1        B   10\n"
     ]
    }
   ],
   "source": [
    "# 方法二\n",
    "df2 = df.copy()\n",
    "df_result = df2.agg(lambda x:x.repeat(df2['num']))\n",
    "print(f'方法二的结果：\\n{df_result}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
